'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac0303
    Inherits Snac

    Public Sub New()
        MyBase.New(&H3, &H3)
    End Sub

    Private _MaxBuddylistSize As New TlvMaxBuddylistSize
    Public ReadOnly Property MaxBuddylistSize() As TlvMaxBuddylistSize
        Get
            Return _MaxBuddylistSize
        End Get
    End Property

    Private _MaxNumberOfWatchers As New TlvMaxNumberOfWatchers
    Public ReadOnly Property MaxNumberOfWatchers() As TlvMaxNumberOfWatchers
        Get
            Return _MaxNumberOfWatchers
        End Get
    End Property

    Private _MaxOnlineNotifications As New TlvMaxOnlineNotifications
    Public ReadOnly Property MaxOnlineNotifications() As TlvMaxOnlineNotifications
        Get
            Return _MaxOnlineNotifications
        End Get
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Return _MaxBuddylistSize.CalculateTotalSize + _MaxNumberOfWatchers.CalculateTotalSize + _MaxOnlineNotifications.CalculateTotalSize
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Snac.SizeFixPart

        Do While index < data.Count
            Dim desc As TlvDescriptor = TlvDescriptor.GetDescriptor(index, data)

            Select Case desc.TypeId
                Case &H1
                    _MaxBuddylistSize.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H2
                    _MaxNumberOfWatchers.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H3
                    _MaxOnlineNotifications.Deserialize(data.GetRange(index, desc.TotalSize))
            End Select

            index += desc.TotalSize
        Loop

        SetTotalSize(index)
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Throw New NotImplementedException
    End Function
End Class

Public Class TlvMaxBuddylistSize
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H1)
    End Sub

    Private _MaxNumberOfContactListEntries As Integer
    Public Property MaxNumberOfContactListEntries() As Integer
        Get
            Return _MaxNumberOfContactListEntries
        End Get
        Set(ByVal value As Integer)
            _MaxNumberOfContactListEntries = value
        End Set
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _MaxNumberOfContactListEntries = ByteConverter.ToUInt16(data.GetRange(index, 2))
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_MaxNumberOfContactListEntries)))

        Return data
    End Function

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function
End Class

Public Class TlvMaxNumberOfWatchers
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H2)
    End Sub

    Private _MaxNumberOfWatchers As Integer
    Public Property MaxNumberOfWatchers() As Integer
        Get
            Return _MaxNumberOfWatchers
        End Get
        Set(ByVal value As Integer)
            _MaxNumberOfWatchers = value
        End Set
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _MaxNumberOfWatchers = ByteConverter.ToUInt16(data.GetRange(index, 2))
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_MaxNumberOfWatchers)))

        Return data
    End Function

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function
End Class

Public Class TlvMaxOnlineNotifications
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H3)
    End Sub

    Private _MaxOnlineNotifications As Integer
    Public Property MaxOnlineNotifications() As Integer
        Get
            Return _MaxOnlineNotifications
        End Get
        Set(ByVal value As Integer)
            _MaxOnlineNotifications = value
        End Set
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _MaxOnlineNotifications = ByteConverter.ToUInt16(data.GetRange(index, 2))
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_MaxOnlineNotifications)))

        Return data
    End Function

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function
End Class